<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" maxlength="11" v-model="addressData.phone" placeholder="收货人手机号码"
			 placeholder-class="placeholder" />
		</view>
		<view @click="chooseLocation" class="row b-b">
			<text class="tit">省市区</text>
			<text class="input">
				<block v-if="addressData.province&&addressData.city&&addressData.area">
					{{addressData.province}}{{addressData.city!='市辖区'?addressData.city:''}}{{addressData.area}}
				</block>
				<block v-if="!addressData.province">
					请选择省市区
				</block>
			</text>
			<text class="yticon icon-shouhuodizhi"></text>
		</view>
		<view class="row b-b">
			<text class="tit tit1">地址</text>
			<input class="input" type="text" v-model="addressData.remark" placeholder="请填写详细地址" placeholder-class="placeholder" />
		</view>

		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.isDefault" color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm">提交</button>

		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor="#007AFF"></simple-address>
	</view>
</template>

<script>
	import simpleAddress from '@/components/simple-address/simple-address.vue';
	export default {
		components: {
			simpleAddress
		},
		data() {
			return {
				cityPickerValueDefault: [0, 0, 0],
				addressData: {
					name: '', //收货人
					phone: '', //手机号
					province: '', //省
					city: '', //市
					area: '', //区
					remark: '', //备注
					isDefault: false
				},
				manageType: 'add', //add-新增 edit-编辑
			}
		},
		onLoad(option) {
			let title = '新增收货地址';
			if (option.type === 'edit') {
				title = '编辑收货地址'

				this.addressData = JSON.parse(option.data)
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title: title
			})
		},
		methods: {
			//-------------------------------------------------点击-----------------------------------------------
			//-------------------------------------------------函数-----------------------------------------------
			/**
			 * 新建收货地址
			 */
			createAddress: function() {
				let than = this;
				let addressData = than.addressData;
				let data = {
					userCode: uni.getStorageSync('userInfo').userCode,
					name: addressData.name,
					phone: addressData.phone,
					province: addressData.province,
					city: addressData.city,
					area: addressData.area,
					remark: addressData.remark,
					isDefault: addressData.isDefault ? 1 : 0
				}
				if (than.manageType == 'edit') {
					data.addressId = addressData.id;
				}
				than.$api.myPost({
					url: than.manageType == 'edit' ? 'updateAddress' : 'createAddress',
					data: data,
					success(res) {
						if (res.status == 'success') {
							than.$api.prePage().refreshList();
							than.$api.msg(`地址${than.manageType=='edit' ? '修改': '添加'}成功`);
							setTimeout(() => {
								uni.navigateBack()
							}, 800)
						} else {
							than.$api.msg(`地址${than.manageType=='edit' ? '修改': '添加'}失败`);
						}
					}
				})
			},
			//-------------------------------------------------监听-----------------------------------------------
			//地址选择回调
			onConfirm(e) {
				this.addressData.province = e.labelArr[0];
				this.addressData.city = e.labelArr[1];
				this.addressData.area = e.labelArr[2];
			},
			//默认地址开关
			switchChange(e) {
				this.addressData.isDefault = e.detail.value;
			},

			//地图选择地址
			chooseLocation() {
				this.$refs.simpleAddress.open();
			},

			//提交
			confirm() {
				let data = this.addressData;
				if (!data.name) {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.phone)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (!data.city) {
					this.$api.msg('请选择省市区');
					return;
				}
				if (!data.remark) {
					this.$api.msg('请填写详细地址');
					return;
				}
				this.createAddress();

			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 120upx;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
